package com.xtm.exercise.utils.export;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;

public final class ExcelUploadUtil {

	public static final String getSavePath() {
		return "./upload";
	}

	public static final String XLS_FILE = ".xls";
	public static final String XLSX_FILE = ".xlsx";
	public static final String RAR_FILE = ".rar";
	public static final String ZIP_FILE = ".zip";
	public static final String PNG_FILE = ".png";
	public static final String JPG_FILE = ".jpg";
	public static final String JPEG_FILE = ".jpeg";
	public static final String BMP_FILE = ".bmp";

	public static final Map<String, List<File>> upload(List<MultipartFile> multipartFiles, String module)
			throws Exception {

		File rootPathFile = new File(getSavePath());
		if (!rootPathFile.exists()) {
			rootPathFile.mkdirs();
		}
		String path = rootPathFile.getPath() + "/" + module + "/";

		Map<String, List<File>> savedFiles = new HashMap<String, List<File>>();

		if (null != multipartFiles && multipartFiles.size() > 0) {
			for (MultipartFile multipartFile : multipartFiles) {

				String fileName = multipartFile.getOriginalFilename();
				if (StringUtils.isNotEmpty(fileName)) {

					String lowerFileName = fileName.toLowerCase();
					File folder = new File(path);
					if (!folder.exists()) {
						folder.mkdir();
					}

					File savedFile = new File(folder.getAbsolutePath() + "/" + fileName);
					if (savedFile.exists()) {
						savedFile.delete();
					}
					multipartFile.transferTo(savedFile);

					if (Pattern.matches("[\\S\\s]+.xls[xmbt]*", lowerFileName)||lowerFileName.endsWith(XLSX_FILE)) {
						if (lowerFileName.endsWith(XLS_FILE)) {
							List<File> excelFiles = savedFiles.get(XLS_FILE);
							if (excelFiles == null) {
								excelFiles = new ArrayList<>();
								savedFiles.put(XLS_FILE, excelFiles);
							}
							excelFiles.add(savedFile);
						}else if(lowerFileName.endsWith(XLSX_FILE)){
							List<File> excelFiles = savedFiles.get(XLSX_FILE);
							if (excelFiles == null) {
								excelFiles = new ArrayList<>();
								savedFiles.put(XLSX_FILE, excelFiles);
							}
							excelFiles.add(savedFile);
						}else{
							throw new Exception("暂时不支持  xls,xlsx之外的文件格式");
						}
//					} else if (lowerFileName.endsWith(RAR_FILE)) {
//						List<File> rarFiles = savedFiles.get(RAR_FILE);
//						if (rarFiles == null) {
//							rarFiles = new ArrayList<>();
//							savedFiles.put(RAR_FILE, rarFiles);
//						}
//						rarFiles.add(savedFile);
//					} else if (lowerFileName.endsWith(ZIP_FILE)) {
//						List<File> zipFiles = savedFiles.get(ZIP_FILE);
//						if (zipFiles == null) {
//							zipFiles = new ArrayList<>();
//							savedFiles.put(ZIP_FILE, zipFiles);
//						}
//						zipFiles.add(savedFile);
					} else if (lowerFileName.endsWith(JPG_FILE) || lowerFileName.endsWith(JPEG_FILE)) {
						List<File> jpgFiles = savedFiles.get(JPG_FILE);
						if (jpgFiles == null) {
							jpgFiles = new ArrayList<>();
							savedFiles.put(JPG_FILE, jpgFiles);
						}
						jpgFiles.add(savedFile);
					} else if (lowerFileName.endsWith(PNG_FILE)) {
						List<File> pngFiles = savedFiles.get(PNG_FILE);
						if (pngFiles == null) {
							pngFiles = new ArrayList<>();
							savedFiles.put(PNG_FILE, pngFiles);
						}
						pngFiles.add(savedFile);
					} else {
						throw new Exception("暂时不支持该种文件格式");
					}
				} else {
					throw new Exception("文件名不能为空");
				}
			}
		}

		return savedFiles;
	}

	public static final void removeTmpFile(Map<String, List<File>> files) {

		for (Entry<String, List<File>> entry : files.entrySet()) {
			for (File file : entry.getValue()) {
				if (file.exists()) {
					file.delete();
				}
			}
		}
	}

}